
package com.trial.area.permission.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.trial.area.common.controller.BaseController;
import com.trial.area.permission.entity.PermissionVO;
import com.trial.area.permission.entity.RoleVO;
import com.trial.area.permission.param.PermissionParam;
import com.trial.area.permission.param.RolePermissionParam;
import com.trial.area.permission.service.PermissionService;
import com.trial.area.permission.service.RolePermissionService;

/**
 * 权限信息Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class PermissionController extends BaseController {
    
    private Logger logger = LoggerFactory.getLogger(PermissionController.class);
    
    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RolePermissionService rolePermissionService;
    
    private final static String LIST_PAGE = "/base/permission/permission_list";
    
    private final static String VIEW_PAGE = "/base/permission/permission_view";
    
    private final static String UPDATE_PAGE = "/base/permission/permission_edit";

    private final static String PERMISSION_ROLE_LIST_PAGE = "/base/permission/permission_role_list";

    @RequestMapping(value = "/base/permission/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        PermissionVO permissionVO = new PermissionVO();
        resultMap.put("permissionVO", permissionVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/base/permission/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        PermissionVO permissionVO = permissionService.findById(id);
        resultMap.put("permissionVO", permissionVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/base/permission/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(PermissionVO permissionVO) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer affertCount = 0;
            if (StringUtils.isNotBlank(permissionVO.getPermissionId())) {
                affertCount = permissionService.update(permissionVO);
            } else {
                permissionVO.setPermissionId(UUID.randomUUID().toString());
                affertCount = permissionService.insert(permissionVO);
            }

            if (affertCount != null && affertCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改权限信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改权限信息失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改权限信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改权限信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/base/permission/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = permissionService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除权限信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除权限信息失败");
            }
        } catch (Exception e) {
            logger.error("删除权限信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除权限信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/base/permission/list")
    public ModelAndView queryList(PermissionParam permissionParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<PermissionVO> dataList = permissionService.queryByPage(permissionParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", permissionParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/base/permission/view/{id}")
    public ModelAndView viewConfigInfo(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        PermissionVO permissionVO = permissionService.findById(id);
        resultMap.put("permissionVO", permissionVO);
        return new ModelAndView(VIEW_PAGE, resultMap);
    }

    @RequestMapping(value = "/base/permission/permissionRoleList")
    public ModelAndView queryPermissionRoleList(RolePermissionParam paramMap) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<RoleVO> dataList = rolePermissionService.queryPermissionRoleList(paramMap);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", paramMap);
        return new ModelAndView(PERMISSION_ROLE_LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/base/permission/insertPermissionRole", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String insertPermissionRole(String permissionId, String roleId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            String resultMsg = rolePermissionService.insertPermissionRole(permissionId, roleId);
            
            if (StringUtils.isBlank(resultMsg)) {
                resultMap.put("result", true);
                resultMap.put("message", "修改角色用户成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", resultMsg);
            }
        } catch (Exception e) {
            logger.error("新增或修改权限信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改权限信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/base/permission/deletePermissionRole", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String deletePermissionRole(String permissionId, String roleId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = rolePermissionService.deleteById(roleId, permissionId);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除权限信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除权限信息失败");
            }
        } catch (Exception e) {
            logger.error("删除权限信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除权限信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
}
